package controllers;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import models.Recommend;


public class RecommendConnect {
	boolean hasEntries;
	private int iROWS;
	
	public RecommendConnect(){
		hasEntries = false;
		iROWS = -1;
	}
	public boolean hasEntries(){
		return hasEntries;
	}
	
	public List<HashMap<String,Object>> getCompleteData(String... fieldNames) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<Recommend> q = ofy.query(Recommend.class);             
    		for ( Recommend  f : q.fetch() ) {
    			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    			for ( String fields : fieldNames) 
    			{
    				if ( fields.equalsIgnoreCase("recID") )   			{   itemMap.put("recID", f.getRecID()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("solID") )   			{   itemMap.put("solID", f.getSolID()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("recommendBy") )		{   itemMap.put("recommendBy", f.getRecommendBy()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("recommendON") )		{	itemMap.put("recommendON", f.getRecommendON()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("comment") )			{   itemMap.put("comment", f.getComment()); hasEntries = true; }
    				
    			}    			
				iROWS++;    			
    			objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public List<HashMap<String,Object>> getCompleteData() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<Recommend> q = ofy.query(Recommend.class);
 
    		for ( Recommend f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("recID", f.getRecID());
    				itemMap.put("solID", f.getSolID());
    				itemMap.put("recommendBy", f.getRecommendBy());
    				itemMap.put("recommendON", f.getRecommendON());
    				itemMap.put("comment", f.getComment());        			
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}

	public List<HashMap<String,Object>> getQuery(String fieldName, String Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<Recommend> q = ofy.query(Recommend.class).filter(fieldName, Value);
 
    		for ( Recommend f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("recID", f.getRecID());
    				itemMap.put("solID", f.getSolID());
    				itemMap.put("recommendBy", f.getRecommendBy());
    				itemMap.put("recommendON", f.getRecommendON());
    				itemMap.put("comment", f.getComment());        			
        			
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	
	public List<HashMap<String,Object>> getQuery(String fieldName, long Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		
    		Query<Recommend> q = ofy.query(Recommend.class).filter(fieldName, Value);
 
    		for ( Recommend f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();

    				itemMap.put("recID", f.getRecID());
    				itemMap.put("solID", f.getSolID());
    				itemMap.put("recommendBy", f.getRecommendBy());
    				itemMap.put("recommendON", f.getRecommendON());
    				itemMap.put("comment", f.getComment());        			
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public int getRows(){
		return iROWS;
	}

	public int getCount(String fieldName, long Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = 0;
		
    	try {
    		Query<Recommend> q = ofy.query(Recommend.class).filter(fieldName, Value);
    		for ( Recommend f : q.fetch() ) {   			
    				iROWS++;    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return iROWS;	
	}
	
}